// packageF/others/customerCenter/component/userDetail/revenueDetails/revenueDetails.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywork: {
      type: null
    },
    memberId: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    strUrl: 'plugin.wechat-chat-sidebar.frontend.controller.customer.customer.income-record',
    getConfig: { client: 'work', type: 17 }, //企业微信公共参数
    showTypeSheet: false,
    actions: [],
    recordList: [],
    statistic: [],
    revenueType: 0,
    json: {
      page: 1,
      income_type: '',
      status: ''
    },
    typeText: '',
    loading: false,
    finished: false,
  },
  lifetimes: {
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en,
      });
      this.getData();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      if (this.data.finished && this.data.loading) {
        return;
      }
      let queryObj = {};
      if (this.data.revenueType == 0) {
        queryObj = {
          page: this.data.json.page,
          income_type: this.data.json.income_type,
        };
        if (this.data.keywork == 'customer') {
          this.data.strUrl = 'plugin.customer-center.frontend.controllers.customer.income-record';
          queryObj.member_id = this.data.memberId;
        }
      } else {
        queryObj = {
          page: this.data.json.page,
          status: this.data.json.status,
        };
        if (this.data.keywork == 'customer') {
          this.data.strUrl = 'plugin.customer-center.frontend.controllers.customer.get-withdraw-log';
          queryObj.member_id = this.data.memberId;
        }
      }
      let json = {...queryObj};
      if (this.data.keywork == 'customer') {
        // 客户中心，不走企业微信侧边栏
        json = { ...queryObj }
      }
      this.loading = true;
      let urlStr = app.getNetAddresss(this.data.strUrl);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1 && res.data.data) {
            console.log(res)
            if (this.data.json.page >= res.data.last_page || res.data.data.length < res.data.per_page) {
              this.setData({
                finished: true
              })
            }
            this.data.recordList = this.data.json.page > 1 ? this.data.recordList.concat(res.data.data) : res.data.data;
            if (this.data.actions.length === 0) {
              this.setActions(res.data.income_type);
            }
            this.data.statistic = res.data.get_withdraw;
            this.data.json.page = ++this.data.json.page;
            this.setData({
              recordList: this.data.recordList,
              statistic: this.data.statistic,
              'json.page': this.data.json.page
            })
          } else {
            this.setData({
              finished: true
            })
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    },
    loadMore () {
      this.getData();
    },
    setActions(data) {
      let recordArr = [];
      for (let item in data) {
        recordArr.push({ name: data[item].title, value: data[item].type });
      }
      recordArr.unshift({ name: '默认选项', value: '', color: '#8f8686' });
      this.setData({
        actions: recordArr
      })
    },
    changeRevenueType (e) {
      let name = e.detail.name;
      this.setData({
        revenueType:name,
        'json.income_type':'',
      })
      this.paramsInit();
      this.getData();
    },
    changeTabsStatus (e) {
      let name = e.detail.name;
      this.setData({
        'json.status':name,
        'json.page':1,
        loading:false,
        finished:false,
        recordList:[]
      })
      this.getData();
    },
    search (e) {
      this.paramsInit();
      this.getData();
    },
    paramsInit () {
      this.setData({
        'json.page':1,
        loading:false,
        finished:false,
        recordList:[],
        statistic:[]
      })
    },
    onSelect (e) {
      this.setData({
        typeText : e.detail.name,
        showTypeSheet:false,
        'json.income_type':e.detail.value
      })
    },
    showTypeSheetShow(){
      this.setData({
        showTypeSheet:true
      })
    }
  }
})
